﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using FrameWork.Controls;
using FrameWork.InterFace;
using IContainer = System.ComponentModel.IContainer;


namespace FrameWork.Classes
{
    // TODO: Make System.ComponentModel.Generic.BindingList derived!!
    public class PageCollectionDic
    {
        public PageCollectionDic(FrameWork.InterFace.IContainer container)
            : base()
        {
            Check.Argument.IsNotNull(container, "container");
            this.container = container;
           // pages.Keys
        }
        public Dictionary<string, IPage>.KeyCollection Keys
        {
            get
            {
                return pages.Keys;
            }
        }
        public Dictionary<string, IPage>.ValueCollection Values
        {
            get
            {
                return pages.Values;
            }
        }
        private FrameWork.InterFace.IContainer container;

   //     private List<DirectUIControl> controls = new List<DirectUIControl>();
        private Dictionary<string, IPage> pages = new Dictionary<string, IPage>();

        /// <summary>
        /// Occurs when the collection has changed.
        /// </summary>
        protected virtual void OnNotifyChanged()
        {
            if (NotifyChanged != null) NotifyChanged(this, EventArgs.Empty);
        }
        /// <summary>
        /// Notify that something has happened to the collection 
        /// </summary>
        public void NotifyChange()
        {
            OnNotifyChanged();
        }
        /// <summary>
        /// Occurs when the collection has changed.
        /// </summary>
        public event EventHandler NotifyChanged;
        public void Add(IPage item)
        {
            Check.Argument.IsNotNull(item,"item");
            Check.Argument.IsNotEmpty(item.Name, "item");
            if (pages.ContainsKey(item.Name))
            {
                throw new ArgumentException("不能添加名称重复的页面", "Item");
            }
            
            pages.Add(item.Name, item);
           item.Container = container;
          //  item.Container = null;
            OnControlAdded(item);
            OnNotifyChanged();
        }
        //public void Insert(int index, DirectUIControl control)
        //{
        //    controls.Insert(index, control);
        //    control.Container = container;
        //    OnControlAdded(control);
        //    OnNotifyChanged();
        //}
        public event EventHandler<FrameWork.Event.ControlEventArgs> ControlAdded;
        private void OnControlAdded(IPage control)
        {
            Check.Argument.IsNotNull(control,"control");
        //    Size size = container.Bounds.Size;
          //  control.Container = container;
            control.Scale(container.ScaleFactor);
           // control.OnControlAdded(container);
         //   INotifyControlAdded notify = container as INotifyControlAdded;
     //       if (notify != null) notify.ControlAdded(control);
            if (ControlAdded != null)
            {
                FrameWork.Event.ControlEventArgs e = new FrameWork.Event.ControlEventArgs(control);
                ControlAdded(this, e);
            }
            
        }
        private void OnControlRemoved(IPage control)
        {
            Check.Argument.IsNotNull(control, "control");
            SizeF f = container.ScaleFactor;
            SizeF scale = new SizeF(1f / f.Width, 1f / f.Height);
            control.Container = null;
            control.Scale(scale);
        }
        [Obsolete]
        private void RemoveControls()
        {
            //SizeF f = container.ScaleFactor;
            //SizeF scale = new SizeF(1f / f.Width, 1f / f.Height);
            //foreach (DirectUIControl c in pages.GetEnumerator())
            //{
            //    c.Scale(scale);
            //    c.Container = null;
            //}
        }
        public void Clear()
        {
           // RemoveControls();
            pages.Clear();
            OnNotifyChanged();
        }
        public bool Contains(IPage item)
        {
            Check.Argument.IsNotNull(item, "item");
            return pages.ContainsKey(item.Name);
        }
        public bool Contains(string pageName)
        {
            Check.Argument.IsNotEmpty(pageName, "pageName");
            return pages.ContainsKey(pageName);
        }
        //public void CopyTo(DirectUIControl[] array, int arrayIndex)
        //{
        //    controls.CopyTo(array, arrayIndex);
        //    OnNotifyChanged();
        //}
        public int Count
        {
            get { return pages.Count; }
        }
        public bool IsReadOnly
        {
            get { return false; }
        }
        public bool Remove(string pageName)
        {
            Check.Argument.IsNotEmpty(pageName, "pageName");
            bool result = false;
            if (pages.ContainsKey(pageName))
            {
                OnControlRemoved(pages[pageName]);
                result=  pages.Remove(pageName);
                if (result) OnNotifyChanged();
            }
            return result;
        }
        public Dictionary<string,IPage>.Enumerator GetEnumerator()
        {
            return pages.GetEnumerator();
        }
        public IPage this[string pageName]
        {
            get {
                Check.Argument.IsNotEmpty(pageName, "pageName");
                return pages[pageName];
            }
        
        }

      
    }
}
